﻿#include "LogWidget.h"
#include <QDateTime>
#include "LogModel.h"
#include <QMutex>
#include <QMutexLocker>
#include <QTimer>
#include <QThread>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QApplication>
#include <QDebug>

LogWidget::LogWidget(QWidget* parent) :
	QWidget(parent)
{
    mInfoModel = new LogModel(this);
    mWarnModel = new LogModel(this);
    mErrorModel = new LogModel(this);
    setupUI();
    initConnect();
	h_this = QThread::currentThread();
    QTimer::singleShot(5000, this, [&](){
        onMessageInfo("ssss");
    });
 }

LogWidget::~LogWidget()
{
}

void LogWidget::onInfoClick()
{
	//加锁
	static QMutex mutex;
	QMutexLocker locker(&mutex);
    lstvLog->setStyleSheet("color: rgb(0, 0, 255);");
    mCurLogType = LogType::Info;
    lstvLog->setModel(mInfoModel);
}

void LogWidget::onWarnClick()
{
	//加锁
	static QMutex mutex;
	QMutexLocker locker(&mutex);
    lstvLog->setStyleSheet("color: rgb(172, 100, 50);");
    mCurLogType = LogType::Warn;
    lstvLog->setModel(mWarnModel);
}

void LogWidget::onErrorClick()
{
	//加锁
	static QMutex mutex;
	QMutexLocker locker(&mutex);
    lstvLog->setStyleSheet("color: rgb(255, 0, 0);");
    mCurLogType = LogType::Error;
    lstvLog->setModel(mErrorModel);
}

void LogWidget::updateCount()
{
    pbtnInfo->setText(QString("提示(%1)").arg(mInfoModel->length()));
    pbtnWarn->setText(QString("警告(%1)").arg(mWarnModel->length()));
    pbtnError->setText(QString("错误(%1)").arg(mErrorModel->length()));
}

void LogWidget::setupUI()
{
    pbtnInfo = new QPushButton("Info");
    pbtnWarn = new QPushButton("Warn");
    pbtnError = new QPushButton("Error");
    lstvLog = new QListView();
    lstvLog->setModel(mInfoModel);
    lstvLog->setEditTriggers(QAbstractItemView::NoEditTriggers);
    lstvLog->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel); //按像素移动

    QVBoxLayout* vLayout = new QVBoxLayout();
    this->setLayout(vLayout);
    QHBoxLayout* hLayout = new QHBoxLayout();
    vLayout->addLayout(hLayout);

    hLayout->addWidget(pbtnInfo);
    hLayout->addWidget(pbtnWarn);
    hLayout->addWidget(pbtnError);
    hLayout->addStretch();

    vLayout->addWidget(lstvLog);
}


void LogWidget::initConnect()
{
    connect(pbtnInfo, &QPushButton::clicked, this, &LogWidget::onInfoClick);
    connect(pbtnWarn, &QPushButton::clicked, this, &LogWidget::onWarnClick);
    connect(pbtnError, &QPushButton::clicked, this, &LogWidget::onErrorClick);

    connect(this, &LogWidget::sigMessageInfo, mInfoModel, &LogModel::insert, Qt::BlockingQueuedConnection);
    connect(this, &LogWidget::sigMessageWarn, mWarnModel, &LogModel::insert, Qt::BlockingQueuedConnection);
}

void LogWidget::onMessage(const QString strMsg)
{
	//加锁
	static QMutex mutex;
	QMutexLocker locker(&mutex);
    QString time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
    LogModel* logModel = mInfoModel;
    QString logTypeMsg = "INFO";
    switch (mCurLogType) {
    case LogType::Info:
        logTypeMsg = "INFO";
        logModel = mInfoModel;
		break;
	case LogType::Warn:
        logTypeMsg = "WARN";
        logModel = mWarnModel;
		break;
	case LogType::Error:
        logTypeMsg = "ERROR";
        logModel = mErrorModel;
		break;
	default:
		break;
    }
    QString msg = time + " [" + logTypeMsg + "]  -  " + strMsg;
    logModel->insert(0, msg);
    if (logModel->length() > 500){
        logModel->removeRow(logModel->length() - 1);
    }
    updateCount();
}

void LogWidget::onMessageInfo(const QString strMsg)
{
    qDebug() << "ssss";
    //加锁
    static QMutex mutex;
    QMutexLocker locker(&mutex);
	h_info = QThread::currentThread();
	QString time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
    QString msg = time + " [INFO]  -  " + strMsg;
	if (h_this != h_info){
        emit sigMessageInfo(0, msg);
	}else{
        mInfoModel->insert(0, msg);
	}
    updateCount();
}

void LogWidget::onMessageWarn(const QString strMsg)
{
    //加锁
    static QMutex mutex;
    QMutexLocker locker(&mutex);
	h_warn = QThread::currentThread();
	QString time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
    QString msg = time + " [WARN]  -  " + strMsg;
	if (h_this != h_warn){
        emit sigMessageWarn(0, msg);
	}else{
        mWarnModel->insert(0, msg);
	}
    updateCount();

}

void LogWidget::onMessageError(const QString strMsg)
{
	//加锁
	static QMutex mutex;
	QMutexLocker locker(&mutex);
	h_error = QThread::currentThread();
	QString time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
    QString msg = time + " [ERROR]  -  " + strMsg;
	if (h_this != h_error){
        emit sigMessageError(0, msg);
	}else{
        mErrorModel->insert(0, msg);
	}
    updateCount();

}
